package servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.AnnounceDao;
import dao.UserDao;
import dao.impl.AnnounceDaoImpl;
import dao.impl.UserDaoImpl;
import model.Announces;

/**
 * Servlet implementation class IndexServlet
 */
@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserDao ud;//用户数据库访问类
	private AnnounceDao ad;//主题数据库访问类
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndexServlet() {
        super();
        // TODO Auto-generated constructor stub
        ud = new UserDaoImpl();
        ad = new AnnounceDaoImpl();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			//得到文学类别下的主题列表
			List<Announces> readList = ad.queryAnnListByClassID(3);
			System.out.println(readList.get(0).getAnnBody());
			request.setAttribute("readingList",readList);
			//根据文学类别下的主题列表信息得到此主题下的用户列表
			request.setAttribute("readUserList", ud.getUserListByAnn(readList));
			//得到电影类别下的主题列表
			List<Announces> filmList = ad.queryAnnListByClassID(2);
			request.setAttribute("filmList", filmList);
			//根据电影类别下的主题列表信息得到此主题下的用户列表
			request.setAttribute("filmUserList", ud.getUserListByAnn(filmList));
			//得到音乐类别下的主题列表
			List<Announces> musicList = ad.queryAnnListByClassID(1);
			request.setAttribute("musicList", musicList);
			//根据音乐类别下的主题列表信息得到此主题下的用户列表
			request.setAttribute("musicUserList", ud.getUserListByAnn(musicList));
			
			//得到所有用户列表信息
			request.setAttribute("allUserList", ud.getAllUser());
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		response.sendRedirect("pages/home.jsp");
		request.getRequestDispatcher("pages/home.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
